/**
 * @author brianxia
 * @version 1.0
 * @date 2022-11-16 14:38
 */
public class Demo2 {

    public static int count = 0;  //静态变量，可以被所有线程访问
    public static Object obj1 = new Object();
    public static Object obj2 = new Object();

    public static void main(String[] args) throws InterruptedException {
        String name = Thread.currentThread().getName();
        System.out.println(name);
        int i = 1; //线程私有变量
        int totalCount = 100000;
        Thread thread1 = new Thread(() -> {

            for (int j = 0; j < totalCount; j++) {
                //可重入
                synchronized (Demo2.class) {
                    synchronized(Demo2.class){
                        count++;
                    }

                }
            }

        });
        Thread thread2 = new Thread(() -> {

            for (int j = 0; j < totalCount; j++) {
                synchronized (Demo2.class) {
                    synchronized(Demo2.class){
                        count++;
                    }
                }
            }
        });
        thread1.start();
        thread2.start();

        thread1.join();
        thread2.join();

        System.out.println(count);
    }
}
